﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

using ITAP.Core.Calculations;
using ITAP.ContourBuilder.Components.Dimensions;
using ITAP.ContourBuilder.Components;
using ITAP.Core.Modelling;

namespace ITAP.ContourBuilder.Manipulation
{
    public class CommandAddOffsetFromMainBorderDimension : SerializedCommand
    {
        public CommandAddOffsetFromMainBorderDimension(ContourControl control)
            : base(control)
        {
        }

        public override void ObtainPeripheryState(GlobalPeripheryState state)
        {
            base.ObtainPeripheryState(state);
            if (state.KeyboardState.KeyMode == KeyMode.Down && state.KeyboardState.KeyState.KeyCode == Keys.Escape)
            {
                this.Exit();
                return;
            }

            if (state.MouseState.MouseMode == MouseMode.Down && state.MouseState.State.Button == MouseButtons.Left)
            {
                float toleranceOffsetInPixels = 10;
                float modelOffset =
                    this.control.CoordinateConverter.ScreenMeasureToModelMeasure(toleranceOffsetInPixels, this.control.Width, this.control.Height, this.control.FullRectangle);
                RectangleF containerRect = this.control.ContourContainer.BoundingBox;
                RectangleF outerRect = containerRect;
                outerRect.Inflate(new SizeF(modelOffset, modelOffset));
                RectangleF innerRect = containerRect;
                innerRect.Inflate(new SizeF(-modelOffset, -modelOffset));
                ModelPoint point = this.ScreenPointToModelPoint(state.MouseState.State.Location);
                PointF floatPoint = new PointF((float)point.X, (float)point.Y);
                OffsetFromMainBorderDimensionType type;
                if (!outerRect.Contains(floatPoint) || innerRect.Contains(floatPoint))
                    return;

                if (SimpleEqualProvider.IsEqual(floatPoint.X, containerRect.X, modelOffset))
                    type = OffsetFromMainBorderDimensionType.Left;
                else if (SimpleEqualProvider.IsEqual(floatPoint.X, containerRect.X + containerRect.Width, modelOffset))
                    type = OffsetFromMainBorderDimensionType.Right;
                else if (SimpleEqualProvider.IsEqual(floatPoint.Y, containerRect.Y, modelOffset))
                    type = OffsetFromMainBorderDimensionType.Down;
                else if (SimpleEqualProvider.IsEqual(floatPoint.Y, containerRect.Y + containerRect.Height, modelOffset))
                    type = OffsetFromMainBorderDimensionType.Up;
                else
                    throw new Exception();

                foreach(IContourComponent component in this.control.ContourContainer.InnerComponents)
                    if (component is OffsetFromMainBorderDimension)
                    {
                        if ((component as OffsetFromMainBorderDimension).Type == type)
                            return;
                    }

                OffsetFromMainBorderDimension dimension = (OffsetFromMainBorderDimension)DimensionFactory.CreateOffsetFromMainBorderDimension(this.control.ContourContainer, GlobalData.DimensionTextFont, "", type);
                this.control.ContourContainer.AddInnerComponent(dimension);
                this.control.UpdateModel();
                this.Exit();
                return;
            }
        }

        public override string ToString()
        {
            return "Создать размер отступов от краёв";
        }
    }
}
